package com.battle.service.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.TrainingSoloDetails;
import com.battle.pojo.param.trainingSoloDetails.*;
import com.battle.pojo.vo.trainingSoloDetails.*;
import com.battle.pojo.dto.trainingSoloDetails.*;
import com.battle.service.repository.in.TrainingSoloDetailsRepository;
import com.battle.service.service.in.TrainingSoloDetailsService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * 【描 述】：单人训练服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class TrainingSoloDetailsServiceImpl implements TrainingSoloDetailsService {

    private final TrainingSoloDetailsRepository trainingSoloDetailsRepository;

    public TrainingSoloDetailsServiceImpl(TrainingSoloDetailsRepository trainingSoloDetailsRepository) {
        this.trainingSoloDetailsRepository = trainingSoloDetailsRepository;
    }


    /**
     * 创建单人训练
     *
     * @param param
     */
    @Override
    public void createTrainingSoloDetails(CreateTrainingSoloDetailsParam param) {
        TrainingSoloDetails trainingSoloDetails = new TrainingSoloDetails();
        BeanUtils.copyProperties(param, trainingSoloDetails);
        Long id = IdGenerator.SNOW.generate();
        trainingSoloDetails.setId(id);
        trainingSoloDetailsRepository.create(trainingSoloDetails);
    }

    /**
     * 修改单人训练
     *
     * @param param
     */
    @Override
    public void editTrainingSoloDetails(EditTrainingSoloDetailsParam param) {
        Long id = param.getId();
        TrainingSoloDetails trainingSoloDetails = trainingSoloDetailsRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, trainingSoloDetails);
        trainingSoloDetailsRepository.edit(trainingSoloDetails);
    }

    /**
     * 移除单人训练
     *
     * @param id
     */
    @Override
    public void removeTrainingSoloDetails(Long id) {
        trainingSoloDetailsRepository.removeById(id);
    }

    /**
     * 启动/禁用单人训练
     *
     * @param id
     */
    @Override
    public void enabledTrainingSoloDetails(Long id) {
        TrainingSoloDetails trainingSoloDetails = trainingSoloDetailsRepository.getByIdCheck(id);
        trainingSoloDetails.setIsDisable(!trainingSoloDetails.getIsDisable());
        trainingSoloDetailsRepository.edit(trainingSoloDetails);
    }

    /**
     * 获取单人训练详情
     *
     * @param id
     * @return
     */
    @Override
    public GetTrainingSoloDetailsVo getTrainingSoloDetails(Long id) {
        TrainingSoloDetails trainingSoloDetails = trainingSoloDetailsRepository.getByIdCheck(id);
        GetTrainingSoloDetailsVo vo = new GetTrainingSoloDetailsVo();
        BeanUtils.copyProperties(trainingSoloDetails, vo);
        return vo;
    }

    /**
     * 获取单人训练 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetTrainingSoloDetailsListVo> getTrainingSoloDetailsList(GetTrainingSoloDetailsListParam param) {
        SelectTrainingSoloDetailsDto dto = new SelectTrainingSoloDetailsDto();
        BeanUtils.copyProperties(param, dto);
        if (dto.getSortField()==null){
            dto.setSortField("create_time");
        }
        Page<GetTrainingSoloDetailsListVo> page = trainingSoloDetailsRepository.selectTrainingSoloDetails(dto);
        return new PageResult<>(page.getRecords(), page);
    }

    /**
     * 获取单人训练下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetTrainingSoloDetailsDownListVo> getTrainingSoloDetailsDownList(GetTrainingSoloDetailsDownListParam param) {
        TrainingSoloDetails po = new TrainingSoloDetails();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<TrainingSoloDetails> trainingSoloDetailsList = trainingSoloDetailsRepository.getListByEntity(po);

        List<GetTrainingSoloDetailsDownListVo> list = new ArrayList<>();
        for (TrainingSoloDetails trainingSoloDetails : trainingSoloDetailsList) {
            GetTrainingSoloDetailsDownListVo vo = new GetTrainingSoloDetailsDownListVo();
            BeanUtils.copyProperties(trainingSoloDetails, vo);
            list.add(vo);
        }
        return list;
    }


}



